
#include "bsp_uart.h"
#include "mydefine.h"

#include "apm32f0xx.h"
#include "apm32f0xx_rcm.h"
#include "apm32f0xx_gpio.h"
#include "apm32f0xx_usart.h"
#include "apm32f0xx_misc.h"
//#include "mydebug.h"

//#include "debug_test.h"


USART_T* COM_USART[COMn] = {MINI_COM1, MINI_COM2};
GPIO_T* COM_TX_PORT[COMn] = {MINI_COM1_TX_GPIO_PORT, MINI_COM2_TX_GPIO_PORT};
GPIO_T* COM_RX_PORT[COMn] = {MINI_COM1_RX_GPIO_PORT, MINI_COM2_RX_GPIO_PORT};
const uint32_t COM_USART_CLK[COMn] = {MINI_COM1_CLK, MINI_COM2_CLK};

const RCM_AHB_PERIPH_T COM_TX_PORT_CLK[COMn] = {MINI_COM1_TX_GPIO_CLK, MINI_COM2_TX_GPIO_CLK};

const RCM_AHB_PERIPH_T COM_RX_PORT_CLK[COMn] = {MINI_COM1_RX_GPIO_CLK, MINI_COM2_RX_GPIO_CLK};

const GPIO_PIN_T COM_TX_PIN[COMn] = {MINI_COM1_TX_PIN, MINI_COM2_TX_PIN};

const GPIO_PIN_T COM_RX_PIN[COMn] = {MINI_COM1_RX_PIN, MINI_COM2_RX_PIN};

const GPIO_PIN_SOURCE_T COM_TX_PIN_SOURCE[COMn] = {MINI_COM1_TX_SOURCE, MINI_COM2_TX_SOURCE};

const GPIO_PIN_SOURCE_T COM_RX_PIN_SOURCE[COMn] = {MINI_COM1_RX_SOURCE, MINI_COM2_RX_SOURCE};

const GPIO_AF_T COM_TX_AF[COMn] = {MINI_COM1_TX_AF, MINI_COM2_TX_AF};

const GPIO_AF_T COM_RX_AF[COMn] = {MINI_COM1_RX_AF, MINI_COM2_RX_AF};
/*!
 * @brief       Configures COM port.
 *
 * @param       COM: Specifies the COM port to be configured.
 *              This parameter can be one of following parameters:
 *              @arg COM1
 *              @arg COM2
 *
 * @retval      None
 *
 * @note
 */
void APM_MINI_COMInit(COM_TypeDef COM)
{
    GPIO_Config_T gpioConfig;
    USART_Config_T usartConfigStruct;
    /** Enable GPIO clock */
//    RCM_EnableAHBPeriphClock(COM_TX_PORT_CLK[COM] | COM_RX_PORT_CLK[COM]);
    RCM_EnableAHBPeriphClock(COM_TX_PORT_CLK[COM]);

    /** Enable COM1 or COM2 clock */
    if (COM == COM1)
    {
//        RCM_EnableAPB2PeriphClock(COM_USART_CLK[COM]);
    }
    else
    {
        RCM_EnableAPB1PeriphClock(COM_USART_CLK[COM]);
    }

    /** Connect PXx to USARTx_Tx */
    GPIO_ConfigPinAF(COM_TX_PORT[COM], COM_TX_PIN_SOURCE[COM], COM_TX_AF[COM]);
#if DEBUG_MAI 
    /** Connect PXx to USARTx_Rx */
//    GPIO_ConfigPinAF(COM_RX_PORT[COM], COM_RX_PIN_SOURCE[COM], COM_RX_AF[COM]);
#endif
    /** Configure USART Tx as alternate function push-pull */
    gpioConfig.mode = GPIO_MODE_AF;
    gpioConfig.pin = COM_TX_PIN[COM];
    gpioConfig.speed = GPIO_SPEED_50MHz;
    gpioConfig.outtype = GPIO_OUT_TYPE_PP;
    gpioConfig.pupd = GPIO_PUPD_PU;
    GPIO_Config(COM_TX_PORT[COM], &gpioConfig);

    /** Configure USART Rx as input floating */
#if DEBUG_MAI   
//    gpioConfig.pin = COM_RX_PIN[COM];
//    GPIO_Config(COM_RX_PORT[COM], &gpioConfig);
#endif
    /** MINI_USARTs configured as follow:
    - BaudRate = 115200 baud
    - Word Length = 8 Bits
    - One Stop Bit
    - No parity
    - Hardware flow control disabled (RTS and CTS signals)
    - Receive and transmit enabled
    */
                               //2000000
    usartConfigStruct.baudRate = 2000000;  //115200
    usartConfigStruct.mode     = USART_MODE_TX_RX;
    usartConfigStruct.hardwareFlowCtrl = USART_FLOW_CTRL_NONE;
    usartConfigStruct.parity   = USART_PARITY_NONE;
    usartConfigStruct.stopBits =  USART_STOP_BIT_1;
    usartConfigStruct.wordLength = USART_WORD_LEN_8B;
    USART_Config(COM_USART[COM], &usartConfigStruct);

    /** Enable USART_Interrupt_RXBNEIE */
#if DEBUG_MAI 
//    USART_EnableInterrupt(COM_USART[COM], USART_INT_RXBNEIE);

//    if (COM_USART[COM] == USART1)
//    {
////        NVIC_EnableIRQRequest(USART1_IRQn, 2);
//    }
//    else if (COM_USART[COM] == USART2)
//    {
//        NVIC_EnableIRQRequest(USART2_IRQn, 3);
//    }
#endif

    /** Enable USART */
    USART_Enable(COM_USART[COM]);
}




void USART_Init(void)
{
	APM_MINI_COMInit(COM2);
	
}


void USART1_SendData(uint8_t data)
{
	USART_TxData(DEBUG_USART,data);
	while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET); 
}


int fputc(int ch, FILE *f)
{
        /** send a byte of data to the serial port */
        USART_TxData(DEBUG_USART,(uint8_t)ch);

        /** wait for the data to be send  */
        while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

        return (ch);
}





void USART_Receive_Isr(void)
{
//	uint8_t dat;
  
    if(USART_ReadStatusFlag(USART2, USART_FLAG_OVRE))
    {
        if(USART_ReadStatusFlag(USART2, USART_FLAG_OVRE))
        {
            USART_ClearStatusFlag(USART2,USART_FLAG_OVRE);
        }
    }
    if(USART_ReadStatusFlag(USART2, USART_FLAG_NEF))
    {
        if(USART_ReadStatusFlag(USART2,USART_FLAG_NEF))
        {
            USART_ClearStatusFlag(USART2,USART_FLAG_NEF);
        }
    }
    if(USART_ReadStatusFlag(USART2, USART_FLAG_PEF))
    {
        if(USART_ReadStatusFlag(USART2,USART_FLAG_PEF))
        {
            USART_ClearStatusFlag(USART2,USART_FLAG_PEF);
        }
    }
    if(USART_ReadStatusFlag(USART2, USART_FLAG_FEF))
    {
        if(USART_ReadStatusFlag(USART2,USART_FLAG_FEF))
        {
            USART_ClearStatusFlag(USART2,USART_FLAG_FEF);
        }
    }
    
	if(USART_ReadStatusFlag(MINI_COM2, USART_FLAG_RXBNE) == SET)
	{
//        dat = (uint8_t)USART_RxData(MINI_COM2);
        USART_RxData(MINI_COM2);
		  
//		  debug_data_recive(dat);
//        debug_recv_data(dat);
//        printf("%c",dat);

	}
}


void USART2_IRQHandler(void)
{
    USART_Receive_Isr();

}







